package com.example.springboot_demo2.Controllor;

import com.example.springboot_demo2.Service.UserService;
import com.example.springboot_demo2.entity.Result;
import com.example.springboot_demo2.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserControl {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户信息
     *
     * @return Result 包含所有用户信息的Result对象
     */
    @GetMapping("/SelectAll")
    public Result<List<User>> SelectAll() {
        List<User> allUsers = userService.getAllUsers();
        return Result.success(allUsers);
    }


    /**
     * 插入用户信息
     *
     * @param user 用户信息
     * @return Result 包含插入用户信息的Result对象
     */
    @PostMapping("/InsertUser")
    public Result<User> InsertUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success(user);
    }

    /**
     * 根据id删除用户信息
     *
     * @param user 用户id 默认
     * @return Result 包含删除用户信息的Result对象
     */
    @PutMapping("/UpdateUser")
    public Result<User> UpdateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success(user);
    }

    @DeleteMapping("/DeleteUser/{id}")
    public Result<String> DeleteUser(@PathVariable("id") int id) {
        userService.deleteUserById(id);
        return Result.success("删除成功");

    }
}